//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BlackVarianceCurve.h"
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVarianceTermStructure.h>
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (DateTime referenceDate, Cephei::IVector<DateTime>^ dates, Cephei::IVector<Double>^ blackVolCurve, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ forceMonotoneVariance) : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
    CoVector<DateTime>^ _Cdates;
    CoVector<Double>^ _CblackVolCurve;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phBlackVarianceCurve = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate);
        CoVector<DateTime>^ _Cdates = safe_cast<CoVector<DateTime>^> (dates);
        _Cdates->Lock();
        INativeVector<DateTime>^ _NCIdates = _Cdates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCdates = safe_cast<CDateTimeVector^>(_NCIdates);
        std::vector<QuantLib::Date>& _dates = static_cast<std::vector<QuantLib::Date>&> (_NCdates->GetReference ());
        CoVector<Double>^ _CblackVolCurve = safe_cast<CoVector<Double>^> (blackVolCurve);
        _CblackVolCurve->Lock();
        INativeVector<Double>^ _NCIblackVolCurve = _CblackVolCurve->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCblackVolCurve = safe_cast<CDoubleVector^>(_NCIblackVolCurve);
        std::vector<QuantLib::Volatility>& _blackVolCurve = static_cast<std::vector<QuantLib::Volatility>&> (_NCblackVolCurve->GetReference ());
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        bool _forceMonotoneVariance = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (forceMonotoneVariance) ? (bool)ValueHelper::Convert (forceMonotoneVariance->Value) : true); //4
        _ppBlackVarianceCurve = new boost::shared_ptr<QuantLib::BlackVarianceCurve> (new QuantLib::BlackVarianceCurve ( _referenceDate,  _dates,  _blackVolCurve,  _dayCounter,  _forceMonotoneVariance ));
        SetBlackVarianceTermStructure (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdates != nullptr) _Cdates->Unlock();
        if (_CblackVolCurve != nullptr) _CblackVolCurve->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (boost::shared_ptr<QuantLib::BlackVarianceCurve>& childNative, Object^ owner) : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phBlackVarianceCurve = NULL;
#endif
	_ppBlackVarianceCurve = &childNative;
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (QuantLib::BlackVarianceCurve& childNative, Object^ owner) : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phBlackVarianceCurve = NULL;
#endif
	_ppBlackVarianceCurve = new boost::shared_ptr<QuantLib::BlackVarianceCurve> (&childNative);
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
    _BlackVarianceCurveOwner = owner;
    _BlackVarianceTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (CBlackVarianceCurve^ copy) : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phBlackVarianceCurve = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackVarianceCurve = new boost::shared_ptr<QuantLib::BlackVarianceCurve> (copy->GetShared());
        _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (System::Type^ t) : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phBlackVarianceCurve = NULL;
#endif
	if (!t->IsSubclassOf(CBlackVarianceCurve::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (QuantLib::Handle<QuantLib::BlackVarianceCurve>& childNative, Object^ owner)  : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
	_phBlackVarianceCurve = &childNative;
	_ppBlackVarianceCurve = &static_cast<boost::shared_ptr<QuantLib::BlackVarianceCurve>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
    _BlackVarianceCurveOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (QuantLib::Handle<QuantLib::BlackVarianceCurve> childNative)  : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
	_phBlackVarianceCurve = &childNative;
	_ppBlackVarianceCurve = &static_cast<boost::shared_ptr<QuantLib::BlackVarianceCurve>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (QuantLib::BlackVarianceCurve childNative)  : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phBlackVarianceCurve = NULL;
#endif
	_ppBlackVarianceCurve = new boost::shared_ptr<QuantLib::BlackVarianceCurve> (new QuantLib::BlackVarianceCurve (childNative));
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
}
#endif

Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::~CBlackVarianceCurve ()
{
    if (_ppBlackVarianceCurve != NULL)
    {
	    delete _ppBlackVarianceCurve;
        _ppBlackVarianceCurve = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::!CBlackVarianceCurve ()
{
    if (_ppBlackVarianceCurve != NULL)
    {
	    delete _ppBlackVarianceCurve;
    }
}
QuantLib::BlackVarianceCurve& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::GetReference ()
{
    if (_ppBlackVarianceCurve == NULL) throw gcnew NativeNullException ();
	return **_ppBlackVarianceCurve;
}
boost::shared_ptr<QuantLib::BlackVarianceCurve>& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::GetShared ()
{
    if (_ppBlackVarianceCurve == NULL) throw gcnew NativeNullException ();
	return *_ppBlackVarianceCurve;
}
QuantLib::BlackVarianceCurve* Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::GetPointer ()
{
    if (_ppBlackVarianceCurve == NULL) throw gcnew NativeNullException ();
	return &**_ppBlackVarianceCurve;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackVarianceCurve>& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::GetHandle ()
{
	if (_phBlackVarianceCurve == NULL)
	{
		_phBlackVarianceCurve = new Handle<QuantLib::BlackVarianceCurve> (*_ppBlackVarianceCurve);
	}
	return *_phBlackVarianceCurve;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::HasNative () 
{
	return (_ppBlackVarianceCurve != NULL);
}

Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppBlackVarianceCurve)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBlackVarianceCurve)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVarianceCurve)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVarianceCurve)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVarianceCurve^ Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve_Factory::Create (DateTime referenceDate, Cephei::IVector<DateTime>^ dates, Cephei::IVector<Double>^ blackVolCurve, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ forceMonotoneVariance)
{
    return gcnew CBlackVarianceCurve ( referenceDate,  dates,  blackVolCurve,  dayCounter,  forceMonotoneVariance);
}
